package com.qfedu.hotelssm.controller;

import com.alibaba.excel.EasyExcel;
import com.qfedu.hotelssm.entity.Department;
import com.qfedu.hotelssm.entity.Employee;
import com.qfedu.hotelssm.service.intf.DepartmentService;
import com.qfedu.hotelssm.service.intf.EmployeeService;
import com.qfedu.hotelssm.vo.PageBean;
import com.qfedu.hotelssm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author truman
 * @date 2022/8/28
 */
@RestController
@RequestMapping("/api/dep/")
public class DepartmentController {
    @Autowired
    private DepartmentService service;

    @PostMapping("save.do")
    public R save(Department department) {
        return service.save(department);
    }

    @GetMapping("del.do")
    public R delete(int id) {
        return service.delete(id);
    }

    @PostMapping("update.do")
    public R update(Department department) {
        return service.update(department);
    }

    @GetMapping("page.do")
    public PageBean select(String name,int page, int limit) {
        return service.selete(name, page, limit);
    }
    /**
     * 查询所有api
     */
    @GetMapping("all.do")
    public R all() {
        return service.all();
    }
    /**
     * 根据Id查询
     */
    @GetMapping("byId.do")
    public R selectById(int id) {
        return service.selectByid(id);
    }
    /**
     * 文件下载
     */
    @GetMapping("download.do")
    public void download(HttpServletResponse response) throws IOException {
        List<Department> list = service.download();
        response.setHeader("content-disposition", "attachment;filename=department-"+System.currentTimeMillis()+".xlsx");
        EasyExcel.write(response.getOutputStream(), Department.class).sheet("部门表").doWrite(list);
    }
    /**
     * 批量删除
     */
    @PostMapping("deletes.do")
    public R deletes(int[] id) {
        return service.deletes(id);
    }
    /**
     * 批量导入
     */
    @PostMapping("upload.do")
    public R upload(MultipartFile file) throws IOException {
        return service.upload(file);
    }
}
